package com.app.biz.service.impl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.app.biz.domain.BizCityIndustrialEconomics;
import com.app.biz.mapper.BizSocialElectricityConsumptionFormalMapper;
import com.app.common.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.app.biz.mapper.BizSocialElectricityConsumptionMapper;
import com.app.biz.domain.BizSocialElectricityConsumption;
import com.app.biz.service.IBizSocialElectricityConsumptionService;

/**
 * 全社会用电量Service业务层处理
 * 
 * @author Zhou_S
 * @date 2023-12-28
 */
@Service
public class BizSocialElectricityConsumptionServiceImpl implements IBizSocialElectricityConsumptionService 
{
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private BizSocialElectricityConsumptionMapper bizSocialElectricityConsumptionMapper;

    @Autowired
    private BizSocialElectricityConsumptionFormalMapper bizSocialElectricityConsumptionFormalMapper;

    /**
     * 查询全社会用电量
     * 
     * @param baseId 全社会用电量主键
     * @return 全社会用电量
     */
    @Override
    public BizSocialElectricityConsumption selectBizSocialElectricityConsumptionByBaseId(String baseId)
    {
        return bizSocialElectricityConsumptionMapper.selectBizSocialElectricityConsumptionByBaseId(baseId);
    }

    /**
     * 查询全社会用电量列表
     * 
     * @param bizSocialElectricityConsumption 全社会用电量
     * @return 全社会用电量
     */
    @Override
    public List<BizSocialElectricityConsumption> selectBizSocialElectricityConsumptionList(BizSocialElectricityConsumption bizSocialElectricityConsumption)
    {
        return bizSocialElectricityConsumptionMapper.selectBizSocialElectricityConsumptionList(bizSocialElectricityConsumption);
    }

    /**
     * 新增全社会用电量
     * 
     * @param bizSocialElectricityConsumption 全社会用电量
     * @return 结果
     */
    @Override
    public int insertBizSocialElectricityConsumption(BizSocialElectricityConsumption bizSocialElectricityConsumption)
    {
        bizSocialElectricityConsumption.setBaseId(IdUtil.simpleUUID());
        bizSocialElectricityConsumption.setBaseCreateTime(new Date());
        DateTime datesTimes= DateUtil.parse(bizSocialElectricityConsumption.getBaseMonth(),"yyyy年MM月");
        bizSocialElectricityConsumption.setDates(DateUtil.format(datesTimes,"yyyy-MM"));
        return bizSocialElectricityConsumptionMapper.insertBizSocialElectricityConsumption(bizSocialElectricityConsumption);
    }

    /**
     * 修改全社会用电量
     * 
     * @param bizSocialElectricityConsumption 全社会用电量
     * @return 结果
     */
    @Override
    public int updateBizSocialElectricityConsumption(BizSocialElectricityConsumption bizSocialElectricityConsumption)
    {
        return bizSocialElectricityConsumptionMapper.updateBizSocialElectricityConsumption(bizSocialElectricityConsumption);
    }

    /**
     * 批量删除全社会用电量
     * 
     * @param baseIds 需要删除的全社会用电量主键
     * @return 结果
     */
    @Override
    public int deleteBizSocialElectricityConsumptionByBaseIds(String[] baseIds)
    {
        return bizSocialElectricityConsumptionMapper.deleteBizSocialElectricityConsumptionByBaseIds(baseIds);
    }

    /**
     * 删除全社会用电量信息
     * 
     * @param baseId 全社会用电量主键
     * @return 结果
     */
    @Override
    public int deleteBizSocialElectricityConsumptionByBaseId(String baseId)
    {
        return bizSocialElectricityConsumptionMapper.deleteBizSocialElectricityConsumptionByBaseId(baseId);
    }

    /**
     * @return
     */
    @Override
    public List<String> getUniqueDatesFromDatabase() {
        List<String> uniqueDatesFromDatabase = bizSocialElectricityConsumptionMapper.getUniqueDatesFromDatabase();
        if(!uniqueDatesFromDatabase.isEmpty()){
            //Mysql中去重关键字和排序不能对同一字段同时使用，这里通过代码来去重
            List<String> collect = uniqueDatesFromDatabase.stream().distinct().collect(Collectors.toList());
            collect = collect.stream().map(item -> {
                String[] split = item.split("-");
                return split[0] + "年" + split[1] + "月";
            }).collect(Collectors.toList());
            return collect;
        }
        return uniqueDatesFromDatabase;
    }

    /**
     * <p>
     *     清空所有的全社会用电量数据
     * </p>
     * @return
     */
    @Override
    public void clearAllTableData() {
        bizSocialElectricityConsumptionMapper.clearAllTableData();
    }

    /**
     * @return
     */
    @Override
    public boolean transferDataInOneClick() {
        try{
            List<BizSocialElectricityConsumption> result = bizSocialElectricityConsumptionMapper.selectBizSocialElectricityConsumptionAllData();

            int count = 0;
            List<BizSocialElectricityConsumption> list = new ArrayList<>();
            for(BizSocialElectricityConsumption item : result){
                list.add(item);
                count++;
                if(count >= 100){
                    bizSocialElectricityConsumptionFormalMapper.batchInsertBizSocialElectricityConsumptionFormal(list);
                    list.clear();
                }
            }

            if(!list.isEmpty()){
                bizSocialElectricityConsumptionFormalMapper.batchInsertBizSocialElectricityConsumptionFormal(list);
                list.clear();
            }
            return true;
        }catch (Exception e){
            logger.error("未知错误:{}",e.getMessage());
            return false;
        }
    }
}
